package cn.iocoder.yudao.module.promotion.service.wechatservice;

import cn.iocoder.yudao.module.promotion.controller.admin.wechatservice.vo.WechatServiceCreateReqVO;
import cn.iocoder.yudao.module.promotion.controller.admin.wechatservice.vo.WechatServiceExportReqVO;
import cn.iocoder.yudao.module.promotion.controller.admin.wechatservice.vo.WechatServicePageReqVO;
import cn.iocoder.yudao.module.promotion.controller.admin.wechatservice.vo.WechatServiceUpdateReqVO;
import cn.iocoder.yudao.module.promotion.convert.wechatservice.WechatServiceConvert;
import cn.iocoder.yudao.module.promotion.dal.dataobject.wechatservice.WechatServiceDO;
import cn.iocoder.yudao.module.promotion.dal.mysql.wechatservice.WechatServiceMapper;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import java.util.*;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.infra.enums.ErrorCodeConstants.WECHAT_SERVICE_NOT_EXISTS;

/**
 * 微信客服 Service 实现类
 *
 * @author 超级管理员
 */
@Service
@Validated
public class WechatServiceServiceImpl implements WechatServiceService {

    @Resource
    private WechatServiceMapper wechatServiceMapper;

    @Override
    public Long createWechatService(WechatServiceCreateReqVO createReqVO) {
        // 插入
        WechatServiceDO wechatService = WechatServiceConvert.INSTANCE.convert(createReqVO);
        wechatServiceMapper.insert(wechatService);
        // 返回
        return wechatService.getId();
    }

    @Override
    public void updateWechatService(WechatServiceUpdateReqVO updateReqVO) {
        // 校验存在
        validateWechatServiceExists(updateReqVO.getId());
        // 更新
        WechatServiceDO updateObj = WechatServiceConvert.INSTANCE.convert(updateReqVO);
        wechatServiceMapper.updateById(updateObj);
    }

    @Override
    public void deleteWechatService(Long id) {
        // 校验存在
        validateWechatServiceExists(id);
        // 删除
        wechatServiceMapper.deleteById(id);
    }

    private void validateWechatServiceExists(Long id) {
        if (wechatServiceMapper.selectById(id) == null) {
            throw exception(WECHAT_SERVICE_NOT_EXISTS);
        }
    }

    @Override
    public WechatServiceDO getWechatService(Long id) {
        return wechatServiceMapper.selectById(id);
    }

    @Override
    public List<WechatServiceDO> getWechatServiceList(Collection<Long> ids) {
        return wechatServiceMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<WechatServiceDO> getWechatServicePage(WechatServicePageReqVO pageReqVO) {
        return wechatServiceMapper.selectPage(pageReqVO);
    }

    @Override
    public List<WechatServiceDO> getWechatServiceList(WechatServiceExportReqVO exportReqVO) {
        return wechatServiceMapper.selectList(exportReqVO);
    }

    @Override
    public void closeWechatService(Long id) {
        // 校验存在
        validateWechatServiceExists(id);
        // 更新
        WechatServiceDO updateObj = new WechatServiceDO().setId(id).setStatus((byte) 1);
        wechatServiceMapper.updateById(updateObj);
    }

    @Override
    public List<WechatServiceDO> getWechatService(WechatServiceExportReqVO pageReqVO) {
        return wechatServiceMapper.selectList(pageReqVO);
    }
}
